/* ***** BEGIN LICENSE BLOCK *****
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Netscape Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/NPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is lutsko.com code.
 *
 * The Initial Developer of the Original Code is 
 * James Lutsko.
 * Portions created by the Initial Developer are Copyright (C) 2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the NPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the NPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

package autoteam.ui.dialogs;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import javax.swing.JDialog;

import autoteam.*;
import autoteam.tables.ParserTableModel;


import jxl.*; 

public class ParserDialog extends JDialog  
{
    private static final long serialVersionUID = -8318153392487834616L;
    private static ParserDialog dialog = null;
    private static DataMap dataMap = null;

    /**
     * Set up the dialog.  The first argument can be null,
     * but it really should be a component in the dialog's
     * controlling frame.
     */
    private static void initialize(JFrame frame, File f)
    {
        dialog = new ParserDialog(frame, f);
    }

    private static void initialize(JFrame frame, Sheet s, File f)
    {
        dialog = new ParserDialog(frame, s, f);
    }

    /**
     * Show the initialized dialog.  The first argument should
     * be null if you want the dialog to come up in the center
     * of the screen.  Otherwise, the argument should be the
     * component on top of which the dialog should appear.
     */
    public static DataMap showDialog(JFrame frame, File f) 
    {
	initialize(frame,f); 
	dialog.setLocationRelativeTo(frame);
	dialog.setVisible(true);

        return dataMap;
    }

    /**
     * Show the initialized dialog.  The first argument should
     * be null if you want the dialog to come up in the center
     * of the screen.  Otherwise, the argument should be the
     * component on top of which the dialog should appear.
     */
    public static DataMap showDialog(JFrame frame, Sheet s, File f) 
    {
	initialize(frame,s,f); 
	dialog.setLocationRelativeTo(frame);
	dialog.setVisible(true);

        return dataMap;
    }

    private ParserDialog(JFrame frame, File f)       
    {
	super(frame,"File Parser",true);
	final ParserTableModel parserModel = new ParserTableModel(f); //,comboBox);
	doInitialize(frame,parserModel);
    }

    private ParserDialog(JFrame frame, Sheet s, File f)       
    {
	super(frame,"File Parser",true);
	final ParserTableModel parserModel = new ParserTableModel(s,f); //,comboBox);
	doInitialize(frame,parserModel);
    }

    void doInitialize(JFrame frame, final ParserTableModel parserModel)
    {
	String labelText = "Fields";

	// the editing table
	JComboBox comboBox = new JComboBox(DataMap.fieldNames);
	DefaultComboBoxModel cbModel = (DefaultComboBoxModel) comboBox.getModel();
	cbModel.addElement(DataMap.SkillString);


        final JTable table = new JTable(parserModel);
        table.setPreferredScrollableViewportSize(new Dimension(500, 170));

	TableColumn fieldColumn = table.getColumnModel().getColumn(1);
	DefaultCellEditor cellEditor = new DefaultCellEditor(comboBox);
	cellEditor.setClickCountToStart(2);
	fieldColumn.setCellEditor(cellEditor);

        //buttons
        JButton cancelButton = new JButton("Cancel");
        final JButton okButton = new JButton("OK");
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ParserDialog.dialog.setVisible(false);
		ParserDialog.dataMap = null;
            }
        });
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) 
		{
		    ParserDialog.dataMap = parserModel.getDataMap();
		    ParserDialog.dialog.setVisible(false);
		}
        });
        getRootPane().setDefaultButton(okButton);



        JScrollPane tableScroller = new JScrollPane(table);
        tableScroller.setPreferredSize(new Dimension(250, 180));
        //XXX: Must do the following, too, or else the scroller thinks
        //XXX: it's taller than it is:
        tableScroller.setMinimumSize(new Dimension(250, 180));
        tableScroller.setAlignmentX(LEFT_ALIGNMENT);

        //Create a container so that we can add a title around
        //the scroll pane.  Can't add a title directly to the 
        //scroll pane because its background would be white.
        //Lay out the label and scroll pane from top to button.
        JPanel tablePane = new JPanel();
        tablePane.setLayout(new BoxLayout(tablePane, BoxLayout.Y_AXIS));
        JLabel label = new JLabel(labelText);
        label.setLabelFor(table);
        tablePane.add(label);
        tablePane.add(Box.createRigidArea(new Dimension(0,5)));
        tablePane.add(tableScroller);
        tablePane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

        //Lay out the buttons from left to right.
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
        buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
        buttonPane.add(Box.createHorizontalGlue());
        buttonPane.add(okButton);
        buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
        buttonPane.add(cancelButton);

        //Put everything together, using the content pane's BorderLayout.
        Container contentPane = getContentPane();
        contentPane.add(tablePane, BorderLayout.CENTER);
        contentPane.add(buttonPane, BorderLayout.SOUTH);

        pack();

    }



}
